using Sidvall.CodeGenerator.Xml;
using System.Xml.Linq;

namespace Sidvall.CodeGenerator
{
    public class SettingFileHelper
    {
        #region Public Members

        #region NewDomainModel

        public static DomainModel NewDomainModel(string filePath, string domainObjectName)
        {
            var settings = new SettingFileImportSettings()
            {
                DomainObjectName = domainObjectName,
            };
            settings.SettingFiles.Add(filePath);
            return NewDomainModel(settings);
        }
        public static DomainModel NewDomainModel(SettingFileImportSettings settings)
        {
            if (settings == null)
                return null;

            var domainModel = new DomainModel();
            var item = new DomainObject()
            {
                Name = settings.DomainObjectName,
            };
            domainModel.DatabaseDomainObjects.Add(item);
            foreach (var settingFile in settings.SettingFiles)
            {
                var document = XDocument.Load(settingFile.FilePath);
                var elements = document.Root.Descendants(@"{http://schemas.microsoft.com/VisualStudio/2004/01/settings}Setting");
                foreach (var element in elements)
                {
                    var domainObjectProperty = new DomainObjectProperty()
                    {
                        Comment = element.GetAttributeValue("Scope"),
                        DefaultValue = element.GetElementValue("Value"),
                        IsComputed = false,
                        IsComputedInDataStore = false,
                        IsIdentity = false,
                        IsIdentityInDataStore = false,
                        IsKey = false,
                        IsNullable = false,
                        IsNullableInDataStore = false,
                        MaxLength = null,
                        MaxDecimalCount = null,
                        Name = element.GetAttributeValue("Name"),
                        NameInDataStore = element.GetAttributeValue("Name"),
                        TypeName = element.GetAttributeValue("Type"),
                        TypeNameInDataStore = element.GetAttributeValue("Type"),
                        ValueFormat = null,
                        ValueFormatInDataStore = null,
                        ConcurrencyMode = ConcurrencyMode.None,
                    };
                    domainObjectProperty.IsValueType = TypeMapper.IsValueType(domainObjectProperty.PropertyType);
                    domainObjectProperty.IsValueTypeInDataStore = domainObjectProperty.IsValueType;
                    if (!item.Properties.Contains(domainObjectProperty.Name))
                    {
                        if (!settings.IsExcluded(domainObjectProperty.Name))
                            item.Properties.Add(domainObjectProperty);
                    }
                }
            }

            return domainModel;
        }

        #endregion

        #endregion
        #region Constructors

        private SettingFileHelper()
        {
        }

        #endregion
    }
}
